package com.unionftech.common.widget

import android.content.Context
import android.graphics.Color
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.widget.ImageView
import android.widget.RelativeLayout
import android.widget.TextView
import com.unionftech.common.R

class RootView : RelativeLayout {
    private lateinit var mContext: Context
    private lateinit var mTitleBar: View
    private lateinit var mIvBack: ImageView
    private lateinit var mTvBackText: TextView
    private lateinit var mTvTitle: TextView
    private lateinit var mIvRight: ImageView
    private lateinit var mTvRightTitle: TextView
    private lateinit var mTitleDivider: View

    private lateinit var mContentView: View

    constructor(context: Context) : this(context, null)
    constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, 0)
    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context,
        attrs,
        defStyleAttr
    ) {
        init(context)
    }

    private fun init(context: Context) {
        mContext = context
        setBackgroundColor(Color.WHITE)
        fitsSystemWindows = true
        //初始化TitleBar
        initTitleBar()
    }

    private fun initTitleBar() {
        mTitleBar = LayoutInflater.from(mContext).inflate(R.layout.layout_common_title_bar, null)
        val params = getTitleBarLayoutParams()
        mTitleBar.visibility = VISIBLE
        addView(mTitleBar, params)
        mIvBack = mTitleBar.findViewById(R.id.iv_back)
        mTvBackText = mTitleBar.findViewById(R.id.tv_back_text)
        mTvTitle = mTitleBar.findViewById(R.id.tv_title)
        mIvRight = mTitleBar.findViewById(R.id.iv_right)
        mTvRightTitle = mTitleBar.findViewById(R.id.tv_right_text)
        mTitleDivider = mTitleBar.findViewById(R.id.title_divider)
    }

    private fun getTitleBarLayoutParams(): LayoutParams {
        val layoutParams =
            LayoutParams(
                LayoutParams.MATCH_PARENT,
                LayoutParams.WRAP_CONTENT
            )
        layoutParams.addRule(ALIGN_PARENT_TOP)
        mTitleBar.id = R.id.title_bar_layout
        return layoutParams
    }

    //-----------------------------------------------导航栏设置---------------------------------------
    fun showTitleBar() {
        mTitleBar.visibility = VISIBLE
    }

    fun hideTitleBar() {
        mTitleBar.visibility = GONE
    }

    fun showBackImg(listener: OnClickListener) {
        showBackImg()
        mIvBack.setOnClickListener(listener)
    }

    fun showBackImg(resId: Int, listener: OnClickListener) {
        showBackImg(resId)
        mIvBack.setOnClickListener(listener)
    }

    fun showBackImg() {
        mIvBack.visibility = VISIBLE
    }

    fun showBackImg(resId: Int) {
        mIvBack.setImageResource(resId)
        showBackImg()
    }

    fun hideBackImg() {
        mIvBack.visibility = GONE
    }

    fun showBackText() {
        mTvBackText.visibility = VISIBLE
    }

    fun setBackText(text: String) {
        mTvBackText.text = text
    }

    fun setBackTextColor(color: Int) {
        mTvBackText.setTextColor(color)
    }

    fun hideBackText() {
        mTvBackText.visibility = GONE
    }

    fun showTitle(title: String) {
        showTitleText()
        mTvTitle.text = title
    }

    fun showTitle(resId: Int) {
        showTitleText()
        mTvTitle.setText(resId)
    }

    fun setTitleColor(color: Int) {
        mTvTitle.setTextColor(color)
    }

    fun showTitleText() {
        showTitleBar()
        mTvTitle.visibility = VISIBLE
    }

    fun setTitleText(text: String) {
        mTvTitle.text = text
    }

    fun hideTitleText() {
        mTvTitle.visibility = GONE
    }

    fun showRightImg() {
        mIvRight.visibility = VISIBLE
    }

    fun showRightImg(resId: Int) {
        mIvRight.setImageResource(resId)
        showRightImg()
    }

    fun showRightImg(resId: Int, listener: OnClickListener) {
        showRightImg(resId)
        mIvRight.setOnClickListener(listener)
    }

    fun hideRightImg() {
        mIvRight.visibility = GONE
    }

    fun showRightText(text: String) {
        mTvRightTitle.visibility = VISIBLE
        mTvRightTitle.text = text
    }

    fun showRightText(resId: Int) {
        mTvRightTitle.visibility = VISIBLE
        mTvRightTitle.setText(resId)
    }

    fun showRightText(text: String, listener: OnClickListener) {
        mTvRightTitle.visibility = VISIBLE
        mTvRightTitle.text = text
        mTvRightTitle.setOnClickListener(listener)
    }

    fun showRightText(resId: Int, listener: OnClickListener) {
        mTvRightTitle.visibility = VISIBLE
        mTvRightTitle.setText(resId)
        mTvRightTitle.setOnClickListener(listener)
    }

    fun hideRightText() {
        mTvRightTitle.visibility = GONE
    }

    fun setRightTextColor(color: Int) {
        mTvRightTitle.setTextColor(color)
    }

    fun showTitleDivider() {
        mTitleDivider.visibility = VISIBLE
    }

    fun hideTitleDivider() {
        mTitleDivider.visibility = GONE
    }

    fun getTitleBar(): View {
        return mTitleBar
    }

    fun addTitleBar(view: View) {
        removeView(mTitleBar)
        mTitleBar = view
        val params = getTitleBarLayoutParams()
        addView(mTitleBar, params)
    }

    fun setTitleBarBgRes(resId: Int) {
        mTitleBar.setBackgroundResource(resId)
    }

    //-----------------------------------------------页面内容设置---------------------------------------


    /**
     * 添加内容页面
     */
    fun addContentView(layoutId: Int) {
        mContentView = LayoutInflater.from(mContext).inflate(layoutId, null)
        LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT).apply {
            addRule(BELOW, mTitleBar.id)
            addRule(ALIGN_PARENT_BOTTOM, TRUE)
            addView(mContentView, this)
        }
    }

    fun addContentView(view: View) {
        mContentView = view
        LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT).apply {
            addRule(BELOW, mTitleBar.id)
            addRule(ALIGN_PARENT_BOTTOM, TRUE)
            addView(mContentView, this)
        }
    }


}